package com.spring.shiro.web;

import com.spring.shiro.common.Log;
import com.spring.shiro.common.controller.BaseController;
import com.spring.shiro.entity.JobLog;
import com.spring.shiro.qo.QueryRequest;
import com.spring.shiro.service.JobLogService;
import com.spring.shiro.utils.ResponseBo;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Slf4j
@Controller
public class JobLogController extends BaseController {

    @Autowired
    private JobLogService jobLogService;

    @Log("获取调度日志信息")
    @PostMapping("/jobLog")
    @RequiresPermissions("jobLog:list")
    public String index() {
        return "job/log/log";
    }

    @GetMapping("/jobLog/list")
    @RequiresPermissions("job:list")
    @ResponseBody
    public Map<String, Object> jobLogList(QueryRequest request, JobLog log) {
        return super.selectByPageNumSize(request, () -> this.jobLogService.findAllJobLogs(log));
    }

    @Log("删除调度日志")
    @RequiresPermissions("jobLog:delete")
    @PostMapping("/jobLog/delete")
    @ResponseBody
    public ResponseBo deleteJobLog(String ids) {
        try {
            this.jobLogService.deleteBatch(ids);
            return ResponseBo.ok("删除调度日志成功！");
        } catch (Exception e) {
            log.error("删除调度日志失败", e);
            return ResponseBo.error("删除调度日志失败，请联系网站管理员！");
        }
    }

}
